<?php

namespace App\Http\Requests\Admin\Panx\Product;

use App\Http\Requests\ApiRequest;
use App\Models\Customer\Language;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\Rule;

/**
 * @property int category
 * @property array category_names
 * @property string|UploadedFile icon
 * @property string|UploadedFile icon_active
 */
class CategoryUpdateRequest extends ApiRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $id = $this->category;
        $language_ids = Language::getLanguageIds();
        $this->language_ids = $language_ids;

        $data = [
//            'name'        => ['required', 'string', 'max:'.self::MAX_STRING, Rule::unique('categories', 'name')->ignore($id)],
//            'const'       => [
//                'required',
//                'string',
//                'max:'.self::MAX_STRING,
//                'regex:/^[0-9A-Za-z-_\s]+$/',
//                Rule::unique('categories', 'const')->ignore($id)
//            ],
            'parent_id'      => 'required|int',
            'priority'       => 'required|int|between:0,10000',
            'has_asset'      => 'required|int|in:0,1',
            'category_names' => 'required|array|size:' . count($language_ids),
        ];

        if (is_file($this->icon)) {
            $data['icon'] = 'required|file|max:1024';
        }

        return $data;
    }

    /**
     * @return array
     */
//    public function messages()
//    {
//        return [
//            'name.*'        => '商品类别必须是唯一',
//            'parent_id.*'   => '商品父级id必须是数值',
//            'const.*'       => '商品类别常量必须是不重复的全大写英文字、- 或 _',
//            'priority.*'    => '商品排序必须是数值',
//            'icon.*'        => '普通图标必须是小于1M的图片',
//            'icon_active.*' => '高亮图标必须是小于1M的图片',
//            'has_asset.*'   => '是否关联素材必须是数字0或1',
//        ];
//    }
}
